package com.wealth.member.web.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.commodity.entity.Comment;
import com.wealth.commodity.service.ICommentService;
import com.wealth.member.entity.Member;
import com.wealth.merchant.enums.MarketEnum;
import com.wealth.order.entity.Order;
import com.wealth.order.entity.OrderVirtual;
import com.wealth.order.service.IOrderItemService;
import com.wealth.order.service.IOrderService;
import com.wealth.order.service.IOrderVirtualService;
import com.wealth.order.vo.OrderVOCenter;
import com.wealth.system.web.controller.BaseController;

import iaas.enums.StatusEnum;
import iaas.utils.ResultObject;

/**
 * 个人中心 -订单
 * 惠生活 ，本地购 ，品牌购 , 积分城 
 * 
 * @author chan
 */
@Controller
@RequestMapping(value="/manage/order")
public class OrderController extends BaseController{

	Logger logger = Logger.getLogger(OrderController.class);

	@Autowired
	private IOrderService orderService;
	
	@Autowired
	private IOrderItemService orderItemService;
	@Autowired
	private IOrderVirtualService orderVirtualService;
	
	@Autowired
	private ICommentService commentService;
	
	//订单列表
	@RequestMapping(value="/status/{status}")
	public String listForm(@PathVariable Integer status,Model model){
		try {
			int count = orderService.countOrder(memberid, status);
			model.addAttribute("count", count);
			model.addAttribute("status", status);
			return "order/list";
		} catch (Exception e) {
			logger.error("订单列表错误", e);
			return "system/500";
		}
	}
	
	//加载更多订单
	@RequestMapping(value="/more",method=RequestMethod.POST)
	@ResponseBody
	public ResultObject list(
			@RequestParam(defaultValue="1") Integer status,
			@RequestParam(defaultValue="-1") Integer index,
			@RequestParam(defaultValue="10") Integer offset) {
		try{
			List<OrderVOCenter> orderList = orderService.ajaxOrder(memberid, status, index, offset);
			 
			return new ResultObject("000", "加载成功", orderList);
		}catch(Exception e){
			return new ResultObject("999", "加载错误", "");
		}
	}
	
	//订单详情
	@RequestMapping(value="/{id}")
	public String infoForm(Model model,@PathVariable Long id){
		try {
			Order order = orderService.get(id);
			model.addAttribute("order", order);
			
			
			Integer isPicked=order.getMerchant().getIsPicked();
			if(order.getMerchant().getMarket().getId()==MarketEnum.LIFE.getValue() ){
				return "order/infoVirtual";
			}else if(isPicked!=null && isPicked==1 ){
				
				OrderVirtual orderVirtual=order.getOrderVirtual();
				if(orderVirtual==null){
					return "order/infoVirtual";
				}
				model.addAttribute("endTime", orderVirtual.getEndTime());
				return "order/infoPicked";
			}
			return "order/info";
		} catch (Exception e) {
			logger.error("订单详情错误,id="+id, e);
			return "system/500";
		}
	}
	
	//订单评价页面
	@RequestMapping(value="/evaluate/{id}")
	public String evaluateForm(Model model,@PathVariable Long id){
		try {
			Order order = orderService.get(id);
			model.addAttribute("order", order);
			
			return "order/evaluate";
		} catch (Exception e) {
			logger.error("订单评价页面错误,id="+id, e);
			return "system/500";
		}
	}
	
	//订单评价
	@RequestMapping(value="/evaluate")
	@ResponseBody
	public ResultObject evaluateForm(Comment comment){
			
		try {
			comment.setMember(new Member(memberid));
			ResultObject rs =  commentService.saveComment(comment);
			return rs;
		} catch (Exception e) {
			return new ResultObject("999", "评价失败", "");
		}
	}
	
	
	//订单签收
	@RequestMapping(value="/sign/{id}")
	public String sign(@PathVariable Long id){
		try {
			Order order = orderService.get(id);
			orderService.sign(order);
			//跳转到未评价
			return "redirect:/manage/order/status/7";
		} catch (Exception e) {
			return "system/500";
		}
	}
	
	@RequestMapping(value="/deletedOrder/{id}",method=RequestMethod.POST)
	@ResponseBody
	public ResultObject deletedOrder(@PathVariable Long id){
		try {
			 orderService.updateStatus(id, StatusEnum.DELETE.getValue()); 
			 return new ResultObject("000", "删除成功", "");
		} catch (Exception e) {
			return new ResultObject("999", "删除失败", "");
		}
	}
	
	
}
